/******************************************************************************
 *
 * MotionViewer
 * Copyright (C) 2007 Sangyoon Lee, Electronic Visualization Laboratory, 
 * University of Illinois at Chicago
 *
 * This software is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser Public License along
 * with this software; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Questions or comments about MotionViewer should be directed to 
 * cavern@evl.uic.edu
 *
 *****************************************************************************/
#include <ogreplatform.h>
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
#include <unistd.h>
#include <Carbon/Carbon.h>
#endif

#include "MotionViewer.h"

#include "CheeseBurgerParadise.h"
#include "ConfigFile.h"
#include "Music.h"

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
	extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
		INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
		int main(int argc, char *argv[])
#endif
		{
			
			
			// load band configuration file
			if ( ! BandConfigFile::openDefaultConfigFile() ) {
				cerr << "Could not open band configuration file " << BandConfigFile::getDefaultConfigFile() << endl;
				return 1;
			}
			
			if ( !BandConfigFile::parse() ) {
				cerr << "Error parsing band configuration file " << BandConfigFile::getDefaultConfigFile() << endl;
				return 1;
			}
			
			cout << "\n** Band configuration file **\n";
			printBandConfig();
			
			// open sound track
			if ( BandConfigFile::hasSoundtrack() ) {
				cout << "Attempting to load soundtrack " << BandConfigFile::getSoundtrackFile() << endl;
				
				// initialize OpenAL
				int argc = 0;
				char *argv[1];
				MusicPlayer::init(argc, argv);
				if (!MusicPlayer::openWAVFile(BandConfigFile::getSoundtrackFile().c_str())) {
					cerr << "Error loading soundtrack." << endl;
					return 1;
				}
				else
				{
					cout << "Soundtrack loaded." << endl;
				}
			
			}
			
			// Create application object
			//MotionViewerApp app;
			CheeseBurgerParadise app;

			try {
				app.go();
			} catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
				MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
				std::cerr << "An exception has occured: " << e.getFullDescription();
#endif
			}
			
			/*// delete soundtrack data if created
			if (BandConfigFile::hasSoundtrack()) {
				MusicPlayer::stopSound();
				//MusicPlayer::killAllData();
			}*/

			return 0;
		}

#ifdef __cplusplus
	}
#endif
